#include "GestureDetector.h"

using namespace cv;
using namespace std;

/** @brief Performs canonical correlation analysis
*   @param Mat &capture, Mat &prototype
*   @return float
*
* Performs canonical correlation analysis between
* two vectors, one being the current gesture, the
* other a prototype gesture
*
*/
float GestureDetector::cca(Mat &capture, Mat &prototype){

    Mat cxx, cyy, cxy, cxyT, cxxI, cyyI, captureT, src, eigenvalues;

    mulTransposed(capture, cxx, true, Mat(), 1, -1);
    mulTransposed(prototype, cyy, true, Mat(), 1, -1);

    transpose(capture, captureT);
    cxy = captureT*prototype;
    transpose(cxy, cxyT);

    invert(cxx, cxxI);
    invert(cyy, cyyI);

    src = (cxxI*cxy)*(cyyI*cxyT);

    eigen(src, eigenvalues, 0, 0);

    float eig = (float)eigenvalues.at<float>(0,0);


    float rho = sqrt(eig);
    return rho;

}
/** @brief Store list values in OpenCV Mat
*   @param list<float>
*   @return Mat
*
* Store list values in matrix for usage as input to
* gesture detection
*
*/
Mat GestureDetector::assignCaptureVals(list<float> vals){
    int size = (int)vals.size();
    Mat capture = Mat_<float>(size,1);
    int cnt = 0;
    list<float>::iterator it;


    for(it = vals.begin(); it!=vals.end();it++){
        capture.at<float>(cnt,0) = (*it);
        cnt++;
    }

    return capture;

}


